package com.chunuo.gulimall.ware.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.chunuo.gulimall.ware.vo.MergeVo;
import com.chunuo.gulimall.ware.vo.PurchaseDoneVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.chunuo.gulimall.ware.entity.PurchaseEntity;
import com.chunuo.gulimall.ware.service.PurchaseService;
import com.chunuo.common.utils.PageUtils;
import com.chunuo.common.utils.R;



/**
 * 采购信息
 *
 * @author liujiamiao
 * @email 2544296058@gmail.com
 * @date 2020-06-25 18:29:36
 */
@Slf4j
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;


    /**
     * 结束采购项 == 可能完成/有异常（如：无货）
     *      信息由采购员发送过来，封装到PurchaseDoneVo对象中
     * @param doneVo
     * @return
     */
    @PostMapping("/done")
    public R finish(@RequestBody PurchaseDoneVo doneVo){

        purchaseService.done(doneVo);

        return R.ok();
    }

    /**
     * 采购员 开始采购
     * @param ids
     * @return
     */
    @RequestMapping("/receive")
    public R receive(@RequestBody List<Long> ids){
        purchaseService.received(ids);

        return R.ok();
    }


    /**
     * 获取采购单还没被采购的数据  处于 新建或已分配状态
     * @param params
     * @return
     */
    @RequestMapping("/unreceive/list")
    public R unreceive(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPageUnreceivePurchase(params);

        return R.ok().put("page", page);
    }

    /**
     * 批量整合采购单
     * @param vo
     * @return
     */
    @PostMapping("/merge")
    public R list(@RequestBody MergeVo vo){
        log.info("mergeVo = {}",vo);
        purchaseService.WithMerge(vo);

        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PurchaseEntity purchase){
        purchase.setCreateTime(new Date());
        purchase.setUpdateTime(new Date());
        purchaseService.save(purchase);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PurchaseEntity purchase){
        purchase.setUpdateTime(new Date());
		purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
